package com.handinfo.android.core.input;

import android.content.Context;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.utils.Tools;

public class DWKey
{
//	public static final int KEY_UP = 1;
//	public static final int KEY_DOWN = 2;
//	public static final int KEY_LEFT = 3;
//	public static final int KEY_RIGHT = 4;
	private static DWKey s_instance = null;

	public static void createInstance()
	{
		s_instance = new DWKey();
	}

	public static DWKey getInstance()
	{
		if (s_instance == null) {
			s_instance = new DWKey();
		}
		return s_instance;
	}

	private boolean isUp, isDown, isLeft, isRight;

	//实体按键
	public void onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
			isUp = true;
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
			isDown = true;
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
			isLeft = true;
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
			isRight = true;
		}
	}

	//实体按键抬起
	public void onKeyUp(int keyCode, KeyEvent event)
	{
		Tools.debugPrintln("keyCode:"+keyCode);
		switch (keyCode) {
			case KeyEvent.KEYCODE_DPAD_UP:
				isUp = false;
				break;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				isDown = false;
				break;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				isLeft = false;
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				isRight = false;
				break;
			case KeyEvent.KEYCODE_BACK:
				keyboardBack();
				break;
			case KeyEvent.KEYCODE_ENTER:
				keyboardEnter();
				break;
			case KeyEvent.KEYCODE_DEL:
				if (m_cache_text != null) {
					String text = m_cache_text.getText();
					if (text != null && !text.equals("")) {
						if (text.substring(text.length() - 1).equals(">")) {
							text = text.substring(0, text.lastIndexOf("</"));
							text = text.substring(0, text.lastIndexOf("<"));
							m_cache_text.setText(text);
						} else {
							m_cache_text.setText(text.substring(0, text.length() - 1));
						}
					}
				}
				break;
		}
	}

	/**
	 * 游戏逻辑
	 */
	public void logic(int x, int y, int speed)
	{
		if (isUp) {
			y -= speed;
		}
		if (isDown) {
			y += speed;
		}
		if (isLeft) {
			x -= speed;
		}
		if (isRight) {
			x += speed;
		}
	}

//	public String m_input_text = null;
//	public String m_cache_text = null;

	public void keyboardEnter()
	{
		if (DWGameManager.getInstance().getGameView() != null && !DWGameManager.getInstance().getGameView().isFocused()) {
			DWGameManager.getInstance().getGameView().requestFocus();
		}
		m_input_text = DWGameManager.getInstance().getInputString();
//		if (m_cache_text != null) {
//			m_input_text = m_cache_text.getText();
//			m_cache_text.setText(null);
//		}
//		if (m_imm != null && m_imm.isActive()) {
//			m_imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
//			m_imm = null;
//			if (m_keyboard_frame != null && DWControlsManager.getInstance().contains(m_keyboard_frame)) {
//				DWControlsManager.getInstance().removeControl(m_keyboard_frame);
//			}
//		}
	}

	public void keyboardBack()
	{
		if (DWGameManager.getInstance().getGameView() != null && !DWGameManager.getInstance().getGameView().isFocused()) {
			DWGameManager.getInstance().getGameView().requestFocus();
		}
//		m_input_text = null;
//		if (m_cache_text != null) {
//			m_cache_text.setText(null);
//		}
//		if (m_imm != null && m_imm.isActive()) {
//			m_imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
//			m_imm = null;
//			if (m_keyboard_frame != null && DWControlsManager.getInstance().contains(m_keyboard_frame)) {
//				DWControlsManager.getInstance().removeControl(m_keyboard_frame);
//			}
//		}
	}

//	private InputMethodManager m_imm;

//	public void getKeyboard()
//	{
//		m_imm = (InputMethodManager)DWGameManager.getInstance().getCurrentActivity().getBaseContext()
//				.getSystemService(Context.INPUT_METHOD_SERVICE);
//		m_imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
//		showKeyboardBackground();
//	}
	
//	private DWFrame m_keyboard_frame = null;
	public String m_input_text = null;
	public DWTextbox m_cache_text = null;
	public String m_control_text = null;
//	public byte m_input_type = DWCharsDetection.CHARS_TYPE_ALL;
//	private void showKeyboardBackground() {
//		if (m_keyboard_frame == null) {
//			m_keyboard_frame = new DWFrame();
//			m_keyboard_frame.setShowWideHigh(DWGameManager.Screen_Width, DWGameManager.Screen_Height);
//			m_keyboard_frame.setClickClose(false);
//			
//			DWBackground bg = new DWBackground(m_keyboard_frame.getShowWidth() - 100, (int)(DWFont.getDefaultFont().getFontHeight() * 11));
//			bg.setNearAnchor(m_keyboard_frame, Tools.LEFT_TOP, Tools.LEFT_TOP);
//			bg.setFill(true);
//			bg.setFillColor(Color.WHITE);
//			bg.setShowCorner(false);
//			m_keyboard_frame.addControl(bg);
//			
//			m_cache_text = new DWTextbox(m_control_text, bg.getShowWidth() - 20, bg.getShowHeight());
//			m_cache_text.setNearAnchor(bg, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
//			m_keyboard_frame.addControl(m_cache_text);
//			
//			DWBackground abg = new DWBackground(100, bg.getShowHeight());
//			abg.setNearAnchor(bg, Tools.LEFT_TOP, Tools.RIGHT_TOP);
//			abg.setFill(true);
//			m_keyboard_frame.addControl(abg);
//			
//			DWButton but = new DWButton("完成", UIWindows.createImage("/img/newui/anniu_9.gnp"));
//			but.setTouchZoomIn(10, 50);
//			but.setDownImage(UIWindows.createImage("/img/newui/anniu_9ax.gnp"));
//			but.setNearAnchor(abg, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
//			but.addListener(new DWListener() {
//				public void OnDoubleClick() {
//				}
//				public void OnClick() {
//					keyboardEnter();
//				}
//			});
//			m_keyboard_frame.addControl(but);
//		}
//		m_cache_text.setText(m_control_text);
//		m_input_text = null;
//		DWControlsManager.getInstance().addControl(m_keyboard_frame);
//	}

//	public void drawKeyboardBackground(DWGraphics g)
//	{
//		if (m_imm != null && m_imm.isActive()) {
//			g.setClip(0, 0, DWGameManager.Screen_Width, DWGameManager.Screen_Height);
////			g.setColor(Tools.WHITE);
////			g.fillRect(0, 0, DWGameManager.Screen_Width, DWGameManager.Screen_Height);
//			if (m_cache_text != null) {
//				g.drawString(m_cache_text, Tools.BLACK, 5, 5, Tools.LEFT_TOP);
//			}
//		}
//	}

//	public static int onUp()
//	{
//
//		return KEY_UP;
//	}
//
//	public static int onDown()
//	{
//
//		return KEY_DOWN;
//	}
//
//	public static int onLeft()
//	{
//
//		return KEY_LEFT;
//	}
//
//	public static int onRight()
//	{
//
//		return KEY_RIGHT;
//	}
}
